package com.example.paimengshop;

import com.example.paimengshop.dataclass.Order;
import com.example.paimengshop.mypanes.CartPane;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class orderController implements Initializable {
    @FXML
    private AnchorPane anchorPane;

    @FXML
    private ImageView orderTitle;

    @FXML
    private VBox vBox;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        //控件样式
        orderTitle.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/beautifyImage/orderTitle.png"));

        //加载订单页面
        vBox.setPrefHeight(Data.orders.size() * 200);
        vBox.setAlignment(Pos.CENTER);
        for (int j = Data.orders.size(), i = j - 1; j > 0; j--, i--) {
            HBox hBox = new HBox();
            hBox.setPrefSize(1200, 200);
            hBox.setAlignment(Pos.CENTER_LEFT);
            hBox.setPadding(new Insets(25, 10, 25, 100));
            hBox.setSpacing(10);

            Label num = new Label();
            num.setText(Integer.toString(j)+":");
            num.setPrefSize(20, 150);
            num.setWrapText(true);
            num.setFont(new Font(16));

            Label userName = new Label(Data.currentUser.getUserName());//如果往Order类中再加一个userName属性，就可以不借助当前登陆者姓名设置了
            userName.setPrefSize(180, 150);
            userName.setWrapText(true);
            userName.setFont(new Font(20));

            //加载商品列表的第一张图片
            ImageView imageView = new ImageView();
            imageView.setImage(new Image(Data.orders.get(i).getCommodityList().get(0).getImageURL()));
            imageView.setFitHeight(150);
            imageView.setFitWidth(150);

            Label address = new Label(Data.orders.get(i).getAddress());
            address.setPrefSize(400, 150);
            address.setWrapText(true);
            address.setFont(new Font(20));

            Label price = new Label(Data.orders.get(i).getPayment() + "元");
            price.setPrefSize(180, 150);
            price.setWrapText(true);
            price.setFont(new Font(20));
            price.setStyle("-fx-font-weight:bold");

            hBox.getChildren().addAll(num, userName, imageView, address, price);

            vBox.getChildren().add(hBox);

            //设置鼠标点击事件
            int finalI = i;
            hBox.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent mouseEvent) {

                    anchorPane.getChildren().clear();
                    anchorPane.getChildren().add(getOrderDetail(Data.orders.get(finalI)));

                }
            });
        }

    }

    public AnchorPane getOrderDetail(Order order) {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setPrefSize(1200, 700);

        Button closeBtn = new Button();
        closeBtn.setText("<");
        closeBtn.setLayoutX(10);
        closeBtn.setLayoutY(10);
//        closeBtn.setFont(new Font(20));
//        closeBtn.setStyle("-fx-font-weight:bold");

        Label nameLabel=new Label(Data.currentUser.getUserName());
        nameLabel.setPrefSize(300, 80);
        nameLabel.setLayoutX(120);
        nameLabel.setLayoutY(50);
        nameLabel.setFont(new Font(20));

        Label address=new Label(order.getAddress());
        address.setPrefSize(400, 80);
        address.setLayoutX(520);
        address.setLayoutY(50);
        address.setFont(new Font(20));

        ScrollPane scrollPane=new ScrollPane();
        scrollPane.setPrefSize(1000, 400);
        scrollPane.setLayoutX(100);
        scrollPane.setLayoutY(150);
        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

        VBox vBox1=new VBox();
        vBox1.setPrefSize(800, 200*order.getCommodityNumber());

        for(int i=0;i<order.getCommodityNumber();i++) {
            CartPane cartPane = new CartPane(order.getCommodityList().get(i));
            cartPane.selectHide();
            vBox1.getChildren().add(cartPane);
        }

        scrollPane.setContent(vBox1);

        Label priceLabel=new Label(order.getPayment()+"元");
        priceLabel.setPrefSize(250, 100);
        priceLabel.setLayoutX(580);
        priceLabel.setLayoutY(750);

        anchorPane.getChildren().addAll(closeBtn, nameLabel, address, scrollPane, priceLabel);

        closeBtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                Stage oldStage=(Stage) closeBtn.getScene().getWindow();

                FXMLLoader fxmlLoader=new FXMLLoader(this.getClass().getResource("order.fxml"));

                Scene scene= null;
                try {
                    scene = new Scene(fxmlLoader.load());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                Stage newStage=new Stage();
                newStage.setTitle("派蒙的周边商铺");
                newStage.setScene(scene);

                oldStage.close();
                newStage.show();
            }
        });

        return anchorPane;
    }

}
